package com.jpcc.vw.onlineRadio.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * 网络工具类
 * Created by wcy on 2016/1/16.
 */
public class NetworkUtils {

    private static Socket s;
    public static boolean isNetworkAvailables(Context context) {
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {

            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            //获取ConnectivityManager对象对应的NetworkInfo对象
            //获取WIFI连接的信息
            NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //获取移动数据连接的信息
            NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (wifiNetworkInfo.isConnected() || dataNetworkInfo.isConnected()) {
                return true;
            }
            return false;
            //API大于23时使用下面的方式进行网络监听
        } else {
            System.out.println("API level 大于23");
            //获得ConnectivityManager对象
            ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            //获取所有网络连接的信息
            Network[] networks = connMgr.getAllNetworks();
            //用于存放网络连接信息
            StringBuilder sb = new StringBuilder();
            //通过循环将网络信息逐个取出来
            for (int i = 0; i < networks.length; i++) {
                //获取ConnectivityManager对象对应的NetworkInfo对象
                NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
                sb.append(networkInfo.getTypeName() + " connect is " + networkInfo.isConnected());
                com.orhanobut.logger.Logger.i("NetworkInfo--->"+sb);
                if (networkInfo.isConnected()) {
                    return true;
                }
            }
            return false;
        }

//        Runtime runtime = Runtime.getRuntime();
//
//        try {
//
//            Process pingProcess = runtime.exec("/system/bin/ping -c 1 www.baidu.com");
//
//            int exitCode = pingProcess.waitFor();
//
//            return (exitCode == 0);
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        }
//
//        return false;






//        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
//        boolean connected = (null != activeNetworkInfo) && activeNetworkInfo.isConnected();
//        Log.d("lubin", "NetworkInfo active = " + connected);
//        if(!connected) return false;
//        boolean routeExists ;
//        try {
//            if(s==null){
//                s = new Socket();
//            }
//            InetAddress host = InetAddress.getByName("8.8.8.8");//全球通用google：8.8.8.8
//            s.connect(new InetSocketAddress(host,53),5000);//google:53
//            routeExists = true;
//        } catch (IOException e) {
//            Log.d("lubin", "IOException = " + e.getMessage());
//            routeExists = false ;
//        } finally {
//            try {
//                s.close();
//                s =null;
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        boolean bConnect = connected && routeExists;
//        Log.d("lubin", "net connect = " + bConnect);
//        return connected && routeExists;
    }

    public static boolean isActiveNetworkMobile(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                return true;
            }
        }
        return false;
    }

    public static boolean isActiveNetworkWifi(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return true;
            }
        }
        return false;
    }
}
